import axios from 'axios'
import router from '../router'
import {
  Loading,
  Message
} from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
axios.defaults.timeout = 60000;
axios.defaults.baseURL = 'http://132.232.13.213:8081/';
// axios.defaults.headers = ("ContentType","application/json;charset=UTF-8")


var loadinginstace
var _this=this;
axios.interceptors.request.use(function (config) {
  /*    var tokens = resp.body.token;
     if (tokens == null) {
      console.log(tokens)
  } else {
      sessionStorage.setItem('tokende', tokens);
  } */
  // 在发送请求之前做些什么
  // console.log(config.data)
 /*  loadinginstace = Loading.service({
    fullscreen: true
  }); */
  NProgress.start();


  return config;
}, function (error) {
  // 对请求错误做些什么
  //loadinginstace.close()
  NProgress.done();
  Message.error({
    message: '加载超时'
  });
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  NProgress.done();
  var responseData = response.data;
  var that = this;
  if (response.data != null) {
    if (responseData.code != 'undefined' && responseData.code != 0 && responseData.code != 200) {
      if (responseData.code > 10000) {
        location.href = "/login";
        Message.error({
          message: response.data.message
        });
        console.log('返回值message='+response.data.message);
      };
    }
  }
  return response;
}, function (error) {
  // 对响应错误做点什么
  if (error && error.response) {
    switch (err.response.status) {
      case 404:
        Message.error({
          message: `请求地址出错`
        });
        this.$router.push('/404');
        break
      default:
    }
  }
  return Promise.reject(error);
});

export default {
  post(url,data,callback) {
    return axios.post(url, data,callback).then(function (response) {
      callback(response.data)
    }).catch((error) => {
      callback(error)
    })
  },

  get(url, data,callback) {
    return axios.get(url, data,callback).then(function (response) {
      callback(response.data)
    }).catch((error) => {
      callback(error)
    })
  },
  put(url, params, callback) {

    return axios.put(url, params ,callback) .then(function (response) {
        callback(response.data)
      }
    ).catch((error) => {
      callback(error)
      })

    return axios.put(url, params ,callback) .then(function (response) {
        callback(response.data)
      }
    ).catch((error) => {
        callback(error)
      })

    return axios.put(url, params, callback).then(function (response) {
      callback(response.data)
    }).catch((error) => {
      callback(error)
    })

  },
  delete(url, params, callback) {
    return axios.delete(url, params, callback).then(function (response) {
      callback(response.data);
    }).catch((error) => {
      callback(error)
    })
  },
  patch(url,params,callback){
    return axios.patch(url,params,callback).then(function (response) {
      callback(response.data);
    }).catch((error) => {
      callback(error)
    })
    }

}
